package com.example.movie

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.common.entity.MovieVideoEntity
import com.bumptech.glide.Glide
import com.example.base.RouterPath
import com.example.common.mvi.BaseMviFragment
import com.example.movie.adapter.MoviePagingDataAdapter
import com.example.movie.databinding.FragmentMovieListBinding
import com.example.movie.vm.MovieViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = RouterPath.PATH_FRAGMENT_MOVIE_LIST)
class MovieListFragment : BaseMviFragment<FragmentMovieListBinding>() {
    @Autowired
    @JvmField
    var tabId: Int = -1

    private lateinit var movieListAdapter:MoviePagingDataAdapter

    private val mViewModel :MovieViewModel by activityViewModels()

    override fun initViews() {
        ARouter.getInstance().inject(this)
        movieListAdapter = MoviePagingDataAdapter()
        mDataBinding.rvMovie.apply {
            layoutManager = GridLayoutManager(requireContext(),2)
            adapter = movieListAdapter
        }
        movieListAdapter.setItemClickListener(object:MoviePagingDataAdapter.MovieItemClickListener{
            override fun onItemChildClick(v: View, position: Int, entity: MovieVideoEntity) {
                ARouter.getInstance().build(RouterPath.PATH_DETAIL_PAGE)
                    .withSerializable("movieEntity",entity)
                    .navigation()
            }
        })

        mDataBinding.refreshLayout.setOnRefreshListener{
            movieListAdapter.refresh()
        }

        //设置RecyclerView固定布局和缓存的大小
        mDataBinding.rvMovie.apply {
            setHasFixedSize(true)
            isNestedScrollingEnabled = false
            setItemViewCacheSize(20)
            //关闭Item动画
            (itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        }

        var cachePool = RecyclerView.RecycledViewPool().apply {
            setMaxRecycledViews(0,5)//其中ViewType必须和适配器中的Item类型保持一致
        }
        mDataBinding.rvMovie.setRecycledViewPool(cachePool)
        mDataBinding.rvMovie.addOnScrollListener(object:RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {

                if(newState == RecyclerView.SCROLL_STATE_IDLE){
                    Glide.with(this@MovieListFragment).resumeRequests()
                }else{
                    Glide.with(this@MovieListFragment).pauseRequests()
                }
            }
        })
        loadDataWithPading3()
    }
    private fun loadDataWithPading3(){
        lifecycleScope.launch {

            if(tabId == -1){
                mViewModel.loadRecommendVideo().collectLatest {
                    if(mDataBinding.refreshLayout.isRefreshing){
                        mDataBinding.refreshLayout.finishRefresh()
                    }
                    //提交数据给适配器
                    movieListAdapter.submitData(it)
                }
            }else{
                mViewModel.loadMutilVideoById(tabId).collectLatest {
                    if (mDataBinding.refreshLayout.isRefreshing){
                        mDataBinding.refreshLayout.finishRefresh()
                    }
                    //提交数据到适配器
                    movieListAdapter.submitData(it)
                }
            }
        }
    }

    override fun getLayoutId(): Int =R.layout.fragment_movie_list
}